﻿using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.Schema;

namespace ScormModel
{
    [Serializable()]
    public class Resource
    {
        private string _identifier;
        private string _scormType;
        private string _type;
        private string _href;
        private List<FileRef> _listFileRef;
        private Dependency _dependency;
         [NonSerialized]
        private XmlNode _resourceContent;
        [XmlAttribute("identifier")]
        public string Identifier
        {
            get { return this._identifier; }
            set { this._identifier = value; }
        }
        [XmlAttribute("scormType", Namespace = "http://www.adlnet.org/xsd/adlcp_v1p3")]
        public string ScormType
        {
            get { return this._scormType; }
            set { this._scormType = value; }
        }
        [XmlAttribute("type")]
        public string Type
        {
            get { return this._type; }
            set { this._type = value; }
        }
        [XmlAttribute("href")]
        public string Bref
        {
            get { return this._href; }
            set { this._href = value; }
        }
        [XmlElement("file")]
        public List<FileRef> ListFileRef
        {
            get { return this._listFileRef; }
            set { this._listFileRef = value; }
        }
        [XmlElement("dependency")]
        public Dependency Dependency
        {
            get { return this._dependency; }
            set { this._dependency = value; }
        }
        
        [XmlIgnoreAttribute]
        public XmlNode ResourceContent
        {
            get { return this._resourceContent; }
            set { this._resourceContent = value; }
        }
        public void buildResource()
        {
            XmlAttributeCollection attList = _resourceContent.Attributes;
            for (int i = 0; i < attList.Count; i++)
            {
                if (attList[i].Name == "identifier")
                {
                    _identifier = attList[i].Value;
                }
                if (attList[i].Name == "href")
                {
                    _href = attList[i].Value;						
                }
                if (attList[i].Name == "adlcp:scormType")
                {
                    _scormType = attList[i].Value;//sco hay asset
                }
                if (attList[i].Name == "type")//webcontent
                {
                    _type = attList[i].Value;
                }
            }
            XmlElement element = (XmlElement)_resourceContent;
            XmlNodeList file_list = element.GetElementsByTagName("file");
            _listFileRef = new List<FileRef>();
            for (int i = 0; i < file_list.Count; i++)
            {
                //MyFile newFile = new MyFile(file_list[i]);
                //fileList.Add(newFile);
                FileRef fileRef = new FileRef();
                fileRef.FileRefContent = file_list[i];
                fileRef.buildFileRef();
                _listFileRef.Add(fileRef);
            }
            XmlNodeList dependency = element.GetElementsByTagName("dependency");
            if (dependency.Count > 0)
            {
                _dependency = new Dependency();
                _dependency.DependencyContent = dependency[0];
                _dependency.buildDependency();
            }

        }
    }
}
